﻿/// <reference path="../base/repositorybase.ts" />
 
module WorkBasket.Repositories {
    export class WorkflowRepository extends WorkBasket.Repositories.RepositoryBase<WorkBasket.Entities.Workflow>
    {

        Objects: Array<WorkBasket.Entities.Workflow>;
        
        constructor() {
            super("Workflow");
        }


        MyMap = function Map(data): Array<WorkBasket.Entities.Workflow>
        {
            this.Objects = [];
            data.forEach(function (o: any) {

                var flds = [];
                o.Fields.forEach(function (x: any) {
                    flds.push(new WorkBasket.Entities.Field(
                        ko.observable(x.FieldID),
                        ko.observable(x.FieldName),
                        ko.observable(x.WorkflowID),
                        ko.observable(x.FieldType),
                        ko.observable(x.FieldInfo)));
                });
 


 


                this.Objects.push(new WorkBasket.Entities.Workflow(
                                    ko.observable(o.WorkflowID),
                                    ko.observable(o.WorkflowName),
                                    ko.observableArray(flds)
                                    )
                                 );
            });
            return this.Objects;
          
        }

        GetAllWorkflows(): Array<Object> 
        {
            var callback = this.MyMap;
            return super.GetAll(callback);
       
           
        }
        Delete(eData: WorkBasket.Entities.Workflow)
        {
            var wf = this.FromKo(eData);
            return this.MyParent().Delete(wf, wf.WorkflowID);
        }
 
        Save(eData: WorkBasket.Entities.Workflow) {
            var wf = this.FromKo(eData);
            return this.MyParent().SaveData(wf, wf.WorkflowID) ;

        }
        FromKo(KoData: WorkBasket.Entities.Workflow): WorkBasket.Entities.Workflow {

            return new WorkBasket.Entities.Workflow(
                ko.unwrap(KoData.WorkflowID),
                ko.unwrap(KoData.WorkflowName),
                ko.unwrap(KoData.Fields)
                );


        }



        private MyParent = function () {
            if (this.super === undefined) {
                this.super = new RepositoryBase("Workflow");
            };
            return this.super;
        }


    }
}